package com.trajan.exercise.algorithm;

/*

   Created with IntelliJ IDEA.

   Ing. Tomáš Herich
   --------------------------- 
   18. 06. 2013
   5:01 PM

*/

public class FibonacciSeries {

    public static void main (String ... args) {

        FibonacciSeries fibonacci = new FibonacciSeries();

        fibonacci.printSeries(20);

        System.out.print("\nFibonacci series: ");
        fibonacci.printSeriesRecursively(0,0, 20);

    }

    public void printSeriesRecursively(int previous1, int previous2, int valuesCount) {

        int next = previous1 + previous2;

        if (next == 0) {
            previous1 = 1;
        }

        System.out.print(next + ", ");


        int recursionsLeft = valuesCount - 1;
        if (recursionsLeft > 0) {
            printSeriesRecursively(next, previous1, recursionsLeft);
        }

    }

    public void printSeries(int valuesCount) {

        int previous2 = 0;
        int previous1 = 0;

        System.out.print("Fibonacci series: ");

        for (int i = 0; i < valuesCount; i++) {

            int next = previous1 + previous2;

            if (next == 0) {
                previous1 = 1;
            }

            previous2 = previous1;
            previous1 = next;

            System.out.print(next + ", ");

        }

    }

}
